package zmee.pur.src.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xw
 * @Date: 2022/11/22/18:09
 * @Description: 议价开标时将本轮待报价改为已开标，上轮已开标改为已关闭
 */
public class NegotiateNegOpenOp extends AbstractOperationServicePlugIn {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        //寻源项目编号
        String billNo = e.getDataEntities()[0].getString("project.billno");
        //轮次
        String turns = e.getDataEntities()[0].getString("turns");
        //待报价的议价单
        QFilter filter = new QFilter("project.billno", "=", billNo).and("turns", "=", turns).and("entrystatus", "=", "A");
        DynamicObject[] analysetools = BusinessDataServiceHelper.load("src_analysetool", "id,entrystatus,supplier,material,entryrcvorg",
                filter.toArray());
        if (analysetools.length > 0) {
            //根据供应商分组
            Map<Object, List<DynamicObject>> listMap =
                    Arrays.stream(analysetools).collect(Collectors.groupingBy(a -> a.get("supplier.id") + "_" + a.get("material.number") + "_" + a.get("entryrcvorg.id")));
            List<DynamicObject> result = new ArrayList<>();
            for (Map.Entry<Object, List<DynamicObject>> entry : listMap.entrySet()) {
                //将状态修改为已开标
                entry.getValue().get(0).set("entrystatus", "C");
                result.add(entry.getValue().get(0));

                //查询上轮议价单将已开标的修改为已关闭
                long supplier = entry.getValue().get(0).getLong("supplier.id");
                long material = entry.getValue().get(0).getLong("material.id");
                String onTheWheelTurns = String.valueOf(Integer.parseInt(turns) - 1);
                QFilter filter1 = new QFilter("project.billno", "=", billNo).and("entrystatus", "=", "C").and("turns", "=",
                        onTheWheelTurns).and("supplier", "=", supplier).and("material", "=", material);
                DynamicObject[] closeTool = BusinessDataServiceHelper.load("src_analysetool", "id,entrystatus", filter1.toArray());
                if (closeTool.length > 0) {
                    for (DynamicObject object : closeTool) {
                        object.set("entrystatus", "D");
                    }
                    SaveServiceHelper.save(closeTool);
                }
            }
            SaveServiceHelper.save(result.toArray(new DynamicObject[0]));

            DynamicObject project = e.getDataEntities()[0].getDynamicObject("project");
            String number = project.getDynamicObject("sourcetype").getString("number");
            long fid = project.getLong("fid");
            if ("C010406".equals(number)) {
                QFilter filter1 = new QFilter("parentid", "=", fid);
                //自动议价方案为竞争性谈判且按物料的重新按未税金额排名
                DynamicObject projectRule = BusinessDataServiceHelper.loadSingle("src_project_rule", "id,extfilter", filter1.toArray());
                String extfilterNumber = projectRule.getDynamicObject("extfilter").getString("number");
                QFilter filter2 = new QFilter("project.billno", "=", billNo).and("turns", "=", turns).and("entrystatus", "=", "C");
                DynamicObject[] analysetoolObjects = BusinessDataServiceHelper.load("src_analysetool", "id,rank,amount,material,supplier,"
                        + "sysresult,result,orderratio,cfmqty,qty", filter2.toArray());
                if ("negotiate004".equals(extfilterNumber)) {
                    Map<Long, List<DynamicObject>> listMaps =
                            Arrays.stream(analysetoolObjects).collect(Collectors.groupingBy(a -> a.getLong("material.id")));
                    for (Map.Entry<Long, List<DynamicObject>> entry : listMaps.entrySet()) {
                        List<DynamicObject> dynamicObjectList = entry.getValue();
                        dynamicObjectList =
                                dynamicObjectList.stream().sorted(Comparator.comparing(d -> d.getBigDecimal("amount"))).collect(Collectors.toList());
                        for (int i = 1; i <= dynamicObjectList.size(); i++) {
                            DynamicObject obj = dynamicObjectList.get(i - 1);
                            obj.set("rank", i);
                            if (i == 1) {
                                obj.set("sysresult", "1");
                                obj.set("result", "1");
                                obj.set("orderratio", new BigDecimal("100"));
                                obj.set("cfmqty", obj.get("qty"));
                            } else {
                                obj.set("sysresult", "3");
                                obj.set("result", "3");
                            }
                        }
                    }
                    SaveServiceHelper.save(analysetoolObjects);
                }
            }
        }
    }
}
